﻿@model MAPDavy.Entities.Matricula

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section JavaScript{
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.datepicker.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.ui.tabs.js")" type="text/javascript"></script>

    <script type="text/javascript">
        $(function () {
            $('.desc').width('450px');

            $("#tblLog tbody tr").hover(
                function () {
                    $(this).addClass("ui-state-highlight");
                },
                function () {
                    $(this).removeClass("ui-state-highlight");
                });

            $("#Student_BirtDate").datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy'
            });

            $("#Log_FechaReunion").datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy'
            });

            $("#Student_FechaIngreso").datepicker({
                changeMonth: true,
                changeYear: true,
                dateFormat: 'dd/mm/yy'
            });

            // $('#Student_BirtDate').datepicker('option', { dateFormat: 'dd/mm/yy' });

            $("#tabs").tabs();

            $("#grade").change(function () {
                LoadSections($("#grade").val());
            });

            $('#reportCard').change(function () {
                var filename = $(this).val();
                if (!/\.pdf$/.test(filename)) {
                    jAlert("Selecciona un archivo tipo PDF", "Mensaje");
                    $('#reportCard').val("")
                }
            });

           /* $('#studentPhoto').change(function () {
                var filename = $(this).val();
                if (!/\.JPG$/.test(filename)) {
                    jAlert("Selecciona un archivo en formato JPG", "Mensaje");
                    $('#studentPhoto').val("");
                }
            });*/

            $("#formLog:ui-dialog").dialog("destroy");

            $("#create-log")
			//.button()
			.click(function () {
			    $("#formLog").dialog("open");
			});

            $("#formLog").dialog({
                height: 470,
                width: 440,
                modal: true,
                resizable: true,
                autoOpen: false,
                buttons: {
                    "Guardar": function () {
                        if ($("#frmLogs").valid()) {
                            $('#frmLogs').submit();
                            $(this).dialog("close");
                        }
                    },
                    Cancel: function () {
                        $(this).dialog("close");
                    }
                }
            });

            $("#frmLogs").validate({
                onkeyup: true,
                onblur: true,
                rules: {
                    Log_FechaReunion: { required: true, date: true },
                    Log_Descripcion: { required: true },
                    fileLog: { required: true }
                }
            });

            $(".deleteLink").click(function (e) {
                e.preventDefault();
                var tr = $(this).parents("tr");
                var logId = tr.find("input:hidden").val();
                jConfirm('Desea Eliminar el Item Seleccionado', 'Confirmación', function (r) {
                    if (r) {
                        $.post(
                               '@Url.Action("Delete", "Logs")',
                                { id: logId },
                                function (ok) {
                                    if (ok) {
                                        $(tr).fadeOut("slow", function () {
                                            $(tr).remove();
                                        });
                                    }
                                    else {
                                        jAlert("No se ha puede Eliminar el Item Seleccionado", "Advertencia");
                                    }
                                },
                                "json"
                        );
                    }
                });
                    });

                    $("#guardar").click(function (e) {

                        var anio = $('#Anio').val();
                        var seccion = $('#SeccionId option:selected').val();

                        $.post('@Url.Action("ExistTutorServiceJson", "Seccion")',
                      { anio: anio, seccionId: seccion },
                      function (ok) {
                          if (ok) {
                              $('#frmMatricula').submit();
                          }
                          else {
                              jAlert("No se puede matricular en esta sección porque aun no tiene un tutor asignado", "Advertencia");
                          }
                      },
                     "json"
               );
                    });
        });

        function LoadSections(id) {
            var dd = document.getElementById("SeccionId");
            dd.options.length = 0;
            dd.options[0] = new Option("Espere...");
            dd.selectedIndex = 0;
            dd.disabled = true;

            // Control de errores

            $("#SeccionId").ajaxError(function (event, request, settings) {
                dd.options[0] = new Option("Selecciona un Grado");
            });

            // Obtenemos los datos...

            $.getJSON(
                '@Url.Action("../Seccion/SeccionServiceJson")', // URL a la acción
                {gradeId: id },                  // Objeto JSON con parámetros
                function (data) {                    // Función de retorno exitoso
                    $.each(data, function (i, item) {
                        dd.options[i] = new Option(item.Seccion, item.Id);
                    });
                    dd.disabled = false;
                });
        }
    </script>
}

<div class="cpanel ui-widget ui-widget-content ui-corner-all">
    <table class="toolbar">
        <tr>
            <td valign="middle" >
                <img src="@Url.Content("~/Content/images/editYear.png")" alt="alumnos" />
            </td>
            <td class="tdHeader"><span class="header">Modificar Matricula</span></td>
            <td class="cerrado"><center>
                <a href="#" id="guardar">
                   <img src="@Url.Content("~/Content/images/save.png")" title="Guardar" alt="Guardar" />
                    <br /><span class="label">Guardar</span>
                </a></center>
            </td>
            <td class="cerrado"><center>
                <a href="#" id="create-log">
                   <img src="@Url.Content("~/Content/images/newLog.png")" title="Registra Log" alt="Registrar Log" />
                    <br /><span class="label">Log</span>
                </a></center>
            </td>
            <td><center>
                <a href="@Url.Action("Index", "Alumnos")">
                    <img src="@Url.Content("~/Content/images/back.png")" title="Candelar" alt="Candelar" />
                    <br /><span class="label">Cancelar</span>
                </a></center>
            </td>
        </tr>
    </table>
</div>    

    <div id="tabs">
	<ul>
		<li><a href="#tabs-1">Información del Alumno</a></li>

		<li><a href="#tabs-2">Log's</a></li>
	</ul>
	<div id="tabs-1">
    @using (Html.BeginForm("Edit", "Alumnos", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmMatricula" }))
    {
        @Html.ValidationSummary(true)
        @Html.HiddenFor(model => model.Id)
        @Html.HiddenFor(model => model.Anio)
        <table border="0" cellpadding="0" cellspacing="0">
            <tr>
                <td valign="top">
                  <fieldset>
                    <legend>Alumno</legend>
                    <div class="editor-label">
                        @Html.LabelFor(model => model.StudentId, "Student Id:")
                    </div>
                    <div class="editor-field">
                        @Html.TextBoxFor(model => model.StudentId, new { @readonly = "readonly" })
                        @Html.ValidationMessageFor(model => model.StudentId)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.FirtsName, "Firts Name:")
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.Student.FirtsName)
                        @Html.ValidationMessageFor(model => model.Student.FirtsName)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.LastName, "Last Name:")
                    </div>
                    <div class="editor-field">
                        @Html.EditorFor(model => model.Student.LastName)
                        @Html.ValidationMessageFor(model => model.Student.LastName)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.Gender, "Gender:")
                    </div>
                    <div class="editor-field">
                        @Html.DropDownList("Student.Gender")
                        @Html.ValidationMessageFor(model => model.Student.Gender)
                    </div>

                    <div class="editor-label">
                        @Html.LabelFor(model => model.Student.BirtDate, "Birt Date:")
                    </div>
                    <div class="editor-field">
                        @Html.TextBox("Student.BirtDate", (Model.Student.BirtDate.HasValue ? Model.Student.BirtDate.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })
                        @Html.ValidationMessageFor(model => model.Student.BirtDate)
                    </div>              
                </fieldset>  
                </td>
                <td valign="top">
                    <fieldset>
                        <legend>Inforamción Académica</legend>        
                        <div class="editor-label">
                        @Html.LabelFor(model => model.Student.DavyId, "Student Admin Plus Id:")
                        </div>
                        <div class="editor-field">
                            @Html.TextBoxFor(model => model.Student.DavyId, new { @readonly = "readonly" })
                            @Html.ValidationMessageFor(model => model.Student.DavyId)
                        </div>
                        <hr />
                        <div class="editor-label">
                            @Html.Label("Grade:")
                        </div>
                        <div class="editor-field">
                            @Html.DropDownList("grade")
                        </div>

                        <div class="editor-label">
                            @Html.LabelFor(model => model.SeccionId,"Class Name")
                        </div>
                        <div class="editor-field">
                            @Html.DropDownList("SeccionId") 
                            @Html.ValidationMessageFor(model => model.SeccionId)
                        </div>
                        <hr />
                        <div class="editor-label">
                            @Html.LabelFor(model => model.Student.FechaIngreso, "Fecha de Ingreso al Colegio")
                        </div>
                        <div class="editor-field">
                            @Html.TextBox("Student.FechaIngreso", (Model.Student.FechaIngreso.HasValue ? Model.Student.FechaIngreso.Value.ToShortDateString() : string.Empty), new { @class = "datePicker" })
                            @Html.ValidationMessageFor(model => model.Student.FechaIngreso)
                        </div>

                         <div class="editor-label">
                            <b>Activo en el Presente Año:</b>
                            @Html.CheckBoxFor(model => model.Activo)                                                 
                        </div>
                        <hr />
                        @if (Model.FileNameReportCard != null)
                        {
                            <div class="editor-label">
                                @Html.Label("Report Card:")
                            </div>
                            <div class="editor-field">                            
                                <a href="@Url.Content(@Model.FileNameReportCard)" target = "_blank">
                                    <img src="@Url.Content("~/Content/images/reportCard.png")" title="ReportCard" alt="ReportCard" />
                                    Ver Existente
                                </a>                     
                            </div>                            
                        }                        
                        <div class="editor-label">
                            @Html.LabelFor(model => model.FileNameReportCard, "Enviar Report Card:")
                        </div>
                        <div class="editor-field">
                            <img src="@Url.Content("~/Content/images/addReportCard.png")" title="AddReportCard" alt="AddReportCard" />
                            <input type="file" id="reportCard" name="reportCard"/>                       
                        </div>                         
                    </fieldset>
                </td>
                <td valign="top">
                    <fieldset>
                        <legend>Foto</legend>
                             @if (Model.Student.FilePhotoName != null)
                             {
                                <img src="@Url.Content(@Model.Student.FilePhotoName)" alt="@Model.Student.FirtsName" />
                             }
                             else
                             { 
                                <img id="foto" src="@Url.Content("~/Content/images/imageCapture.png")" alt="Alternate Text" />
                             }
                            <div class="editor-field">
                                <input type="file" id="studentPhoto" name="studentPhoto"/>
                            </div> 
                    </fieldset>   
                </td>
            </tr>
        </table>
    }
	</div>
	<div id="tabs-2">
        <div id="formLog" title="Registrar Nuevo Log">   
        
		@using (Html.BeginForm("Create", "Logs", FormMethod.Post, new { enctype = "multipart/form-data", id = "frmLogs", name = "frmLogs" }))
  {
            @Html.Hidden("Log.MatriculaId", Model.Id)
            @Html.Hidden("Log.StudentId", Model.StudentId)
            @Html.Hidden("Log.Anio", Model.Anio)

            <div class="editor-label">
                @Html.Label("Log.FechaReunion", "Fecha de Reunion:")
            </div>
            <div class="editor-field">
                @Html.TextBox("Log.FechaReunion", null, new { @class = "required" })<br />
                @Html.ValidationMessage("Log.FechaReunion", "Selecciona la fecha de reunión")
            </div>
            <div class="editor-label">
                @Html.Label("Log.Descripcion", "Descripción:")
            </div>
            <div class="editor-field">
                @Html.TextBox("Log.Descripcion", null, new { @style = "width: 400px;", @class = "required" }) <br />
                @Html.ValidationMessage("Log.Descripcion", "Ingresa una descripción")
            </div>            
            <div class="editor-label">
              @Html.Label("fileLog", "Archivo:")
            </div>
            <div class="editor-field">
                <img src="@Url.Content("~/Content/images/addReportCard.png")" title="AddReportCard" alt="AddReportCard" />
                <input type="file" id="fileLog" name="fileLog" size="38" class="required"/><br />
                @Html.ValidationMessage("fileLog", "Selecciona un Archivo")                       
           </div>
            <div class="editor-label">
                @Html.Label("Log.Comentario", "Comentario:")
            </div>
            <div class="editor-field">
                @Html.TextArea("Log.Comentario", new { @cols = "48" }) 
            </div>  
  }
        </div>
        <br /> <br />
        <table id="tblLog">
            <tr>
                <th class="ui-widget ui-widget-header">
                    Log File
                </th>
                <th class="ui-widget ui-widget-header desc">
                    Descriptión
                </th>
                <th class="ui-widget ui-widget-header">
                    Fecha Reunión
                </th>
                <th class="ui-widget ui-widget-header">
                    Comentario
                </th>
                <th class="ui-widget ui-widget-header"></th>
            </tr>
        @foreach (var item in Model.Logs)
        {
            <tr>
                <td>
                    @Html.Hidden("logId." + item.Id, item.Id)
                    <center>
                    <a href="@Url.Content(item.FileName)" target = "_blank">
                        <img src="@Url.Content("~/Content/images/viewLog.png")" title="Ver Log" alt="Ver log" />
                    </a> 
                    </center>
                </td>
                <td>                    
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
                <td>
                    @item.FechaReunion.ToShortDateString()
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Comentario)
                </td>
                <td>
                    <a href="@Url.Action("Delete", "Log", new { id = item.Id })" class = "deleteLink cerrado">
                        <img src="@Url.Content("~/Content/images/delete.png")" title="Remover Log" alt="Remover Log" /></a>                    
            </tr>
        }

        </table>
	</div>
</div>   
